package cn.bone.async.web;

import cn.bone.async.bean.User;
import cn.bone.async.service.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;

/**
 * Created by MD-01 on 2017/3/28.
 */
@RestController
public class DemoController {

    private Logger logger = LoggerFactory.getLogger("web请求");
    @Resource
    private DemoService demoService;

    @RequestMapping("/block")
    public String block(){
        logger.info("Request received");
        String result = demoService.say("戴泽");
        logger.info("Servlet thread released");
        return result;
    }

    @RequestMapping("/callable")
    public Callable<String> callable(){
        logger.info("Request received");
        String name = "戴泽";
        Callable<String> callable = ()->{
            return demoService.say(name);
        };
        logger.info("Servlet thread released");
        return callable;
    }

    @RequestMapping("/deferredResult")
    public DeferredResult<String> deferredResult(){
        logger.info("Request received");
        String name = "戴泽";
        DeferredResult<String> deferredResult =
                new DeferredResult<>(1000L,"我超时了");
        CompletableFuture.supplyAsync(()->{
            return demoService.say(name);
        }).whenCompleteAsync((result,throwable)->{
            deferredResult.setResult(result);
        });

        logger.info("Servlet thread released");
        return deferredResult;
    }

    @RequestMapping("/async")
    public DeferredResult<String> async(){
        logger.info("Request received");
        String name = "戴泽";
        DeferredResult<String> deferredResult =
                new DeferredResult<>(1000L,"我超时了");

        ListenableFuture<String> execute = demoService.execute();
        execute.addCallback(new ListenableFutureCallback<String>() {
            @Override
            public void onFailure(Throwable ex) {
                logger.info("onFailure");
                deferredResult.setResult("异常了");
            }

            @Override
            public void onSuccess(String result) {
                logger.info("onSuccess");
                deferredResult.setResult(result);
            }
        });
        return deferredResult;
    }

    @RequestMapping("/json")
    public User json(){
        User user = new User();
        user.setAdress("adress");
        user.setAge(20);
        user.setFullName("fullName");
        user.setId(10);
        user.setName("戴泽");
        user.setBrithday(new Date());
        return user;
    }
}
